package com.sdkj.controller.law;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sdkj.base.common.FileDownload;
import com.sdkj.base.common.FileUpload;
import com.sdkj.controller.base.BaseController;
import com.sdkj.entity.Page;
import com.sdkj.service.law.LawApplyService;
import com.sdkj.service.law.LawReviewService;
import com.sdkj.util.Const;
import com.sdkj.util.DateUtil;
import com.sdkj.util.Jurisdiction;
import com.sdkj.util.PageData;
import com.sdkj.util.PathUtil;


/** 
 * 类名称：LawAppController
 * 创建人：HXF
 * 创建时间：2017-05-25
 */
@Controller
@RequestMapping(value="/lawreview")
public class LawReviewController extends BaseController{
	String menuUrl = "lawreview/list.do"; // 菜单地址（权限用）
	@Resource(name="lawApplyService")
	private LawApplyService lawApplyService;
	
	@Resource(name="lawReviewService")
	private LawReviewService lawReviewService;
	
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		logBefore(logger, "列表LawApp");
		//if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try {
			pd=this.getPageData();
			String DIM = pd.getString("DIM");
			String Status = pd.getString("STATUS");
			if(DIM != null && DIM.equals("")){
				DIM = DIM.trim();
				pd.put("DIM", DIM);
			}
			pd.put("STATUS", Status);
			page.setPd(pd);
			List<PageData> varList = lawApplyService.list(page); //列出LawApp列表
			mv.setViewName("law/lawapply/lawapply_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
		
	}
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit(){
		logBefore(logger, "去往审阅页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd =lawReviewService.findById(pd); //根据ID读取
			mv.setViewName("law/lawapply/lawapply_review");
			mv.addObject("msg", "edit");
			mv.addObject("pd",pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/*
	 * 审阅信息
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit()throws Exception{
		logBefore(logger, "审阅");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		PageData pd2 = new PageData();
		pd = this.getPageData();
		if (pd.getString("review_status").equals("1")) {
			pd2.put("review_id", this.get32UUID()); // 主键
			pd2.put("user_id", getCurrentUser().getUSER_ID());
			pd2.put("review_person", getCurrentUser().getNAME());
			pd2.put("dept_id", getCurrentUser().getDepartmentId());
			pd2.put("company_id", getCurrentUser().getCompanyId());
			pd2.put("review_information", pd.getString("review_information"));
			pd2.put("review_status", pd.get("review_status"));
			pd2.put("apply_id", pd.get("apply_id"));
			pd.put("apply_status", 1);
			lawReviewService.save(pd2);
			lawApplyService.update(pd);
		}else {
			pd2.put("review_id", this.get32UUID()); // 主键
			pd2.put("user_id", getCurrentUser().getUSER_ID());
			pd2.put("review_person", getCurrentUser().getNAME());
			pd2.put("dept_id", getCurrentUser().getDepartmentId());
			pd2.put("company_id", getCurrentUser().getCompanyId());
			pd2.put("review_information", pd.getString("review_information"));
			pd2.put("review_status", pd.get("review_status"));
			pd2.put("apply_id", pd.get("apply_id"));
			lawReviewService.save(pd2);
			pd.put("apply_status", 1);
			lawApplyService.update(pd);
		}
		
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	/*
	 * 去往详情界面
	 */
	@RequestMapping(value="/look")
	public ModelAndView look(){
		logBefore(logger, "详情");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = lawReviewService.findByCid(pd);
			mv.setViewName("law/lawapply/lawapply_look");
			mv.addObject("msg","success");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		logBefore(logger, "去新增");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("law/lawapply/lawapply_add");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}
	
	/**
	 * 保存
	 */
	
	@RequestMapping(value="/save")
	public ModelAndView save(MultipartFile file,MultipartFile file2,HttpServletRequest request) throws Exception{
		logBefore(logger, "新增Lawask");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData(request);
		String time = DateUtil.getDay();
		String filePath = null;
		String fileName = null;
		if (null != file && !file.isEmpty()) {
			filePath =PathUtil.getClasspath()+ Const.FILEPATHFILE+time;
			fileName = FileUpload.fileUpload(file, filePath, this.get32UUID());	
		}else {
			System.out.println("附件上传失败");
		}
		String time2 = DateUtil.getDay();
		String filePath2 = null;
		String fileName2 = null;
		if (null != file2 && !file2.isEmpty()) {
			filePath2 =PathUtil.getClasspath()+ Const.FILEPATHFILE+time2;
			fileName2 = FileUpload.fileUpload(file2, filePath2, this.get32UUID());	
		}else {
			System.out.println("相关上传失败");
		}
		pd.put("apply_id", this.get32UUID());	//主键
		pd.put("user_id", getCurrentUser().getUSER_ID());
		pd.put("apply_person", getCurrentUser().getUSERNAME());
		pd.put("dept_id", getCurrentUser().getDepartmentId());
		pd.put("company_id", getCurrentUser().getCompanyId());
		pd.put("apply_attachment",filePath);
		pd.put("apply_related",filePath2);
		pd.put("apply_name", fileName);
		pd.put("related_name", fileName2);
		pd.put("apply_status", 0);
		lawApplyService.save(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	/*
	 * 下载功能
	 */
	@RequestMapping(value="download")
	public void download(HttpServletResponse response)throws Exception{
		logBefore(logger, "下载功能");
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {return;}
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = lawReviewService.findById(pd);
		String filePath = (String) pd.get("apply_attachment");
		String fileName = (String) pd.get("apply_name");
		FileDownload.fileDownload(response, filePath+"/"+fileName, fileName);
	}
	
	@RequestMapping(value="download1")
	public void download1(HttpServletResponse response)throws Exception{
		logBefore(logger, "下载功能");
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {return;}
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = lawReviewService.findById(pd);
		String filePath = (String) pd.get("apply_related");
		String fileName = (String) pd.get("related_name");
		FileDownload.fileDownload(response, filePath+"/"+fileName, fileName);
	}
	
	
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
	
	
	
	
}
